package com.bs.visitservice.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bs.visitservice.entity.Comment;
import com.bs.visitservice.entity.CommentPhoto;
import com.bs.visitservice.service.CommentPhotoService;
import com.bs.visitservice.service.CommentService;
import com.bs.visitservice.utils.Result;
import com.bs.visitservice.utils.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评论管理
 */
@RestController
@RequestMapping("/admin")
public class CommentHandleController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private CommentPhotoService commentPhotoService;
    /**
     * 查询评论
     * @param cond
     * @return
     */
    @RequestMapping(value = "/queryComment",method = RequestMethod.POST)
    public Result queryComment(@RequestBody Map cond){
        if(StringUtils.isEmpty(String.valueOf(cond.get("sellerNo")))){
            cond.put("sellerNo",null);
        }
        cond.put("flag",1);
        List<Map<String, Object>> maps = commentService.querySellerComments(cond);
        Map<String,Object> cond1 = new HashMap<>();
        cond1.putAll(cond);
        cond1.remove("pageIndex");
        List<Map<String, Object>> maps1 = commentService.querySellerComments(cond1);
        maps.forEach(map->{
            Long no = (Long)map.get("no");
            LambdaQueryWrapper<CommentPhoto> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CommentPhoto::getCommentNo,no).orderByAsc(CommentPhoto::getSn);
            List<Map<String, Object>> photoList = commentPhotoService.listMaps(wrapper);
            map.put("photos",photoList);
        });
        Map<String,Object> result = new HashMap<>();
        result.put("total",maps1.size());
        result.put("datas",maps);
        return Result.success(result);
    }

    /**
     * 删除评论
     * @param cond
     * @return
     */
    @RequestMapping(value = "removeComment",method = RequestMethod.POST)
    public Result removeComment(@RequestBody Map cond){
        Integer no = (Integer) cond.get("no");
        boolean isSuc = commentService.removeById(no);
        if(isSuc){
            return Result.success(null);
        }
        return Result.error(null);
    }

    /**
     * 驳回
     * @param cond
     * @return
     */
    @RequestMapping(value = "noPassComment",method = RequestMethod.POST)
    public Result noPassComment(@RequestBody Map cond){
        Comment comment = (Comment) ResultUtil.transObject(cond,Comment.class);
        comment.setResult("0");
        comment.setFlag("0");
        boolean isSuc = commentService.updateById(comment);
        if(isSuc){
            return Result.success(null);
        }
        return Result.error(null);
    }
}
